<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="唯一编码值" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入唯一编码值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高速公路" prop="expressWayName">
        <el-input
          v-model="queryParams.expressWayName"
          placeholder="请输入高速公路"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="桩号千米" prop="expressWayKil">
        <el-input
          v-model="queryParams.expressWayKil"
          placeholder="请输入桩号千米"
          clearable
          type="number"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="桩号米数" prop="expressWayMeter">
        <el-input
          v-model="queryParams.expressWayMeter"
          placeholder="请输入桩号米数"
          clearable
          type="number"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方向类型" prop="direction">
        <el-select v-model="queryParams.direction" placeholder="请选择方向类型" clearable size="small">
          <el-option
            v-for="dict in directionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二维码状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择二维码状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:qrcode:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:qrcode:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:qrcode:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-upload2"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manager:qrcode:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="cyan"
          icon="el-icon-download"
          size="mini"
          @click="handledownload"
        >下载</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-plus"
          size="mini"
          @click="batch = true"
        >批量生成</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
				  accept=".zip"
				  :multiple="false"
				  :show-file-list="false"
				  :action="uploadUrl"
				  :file-list="fileList"
				  :on-success="successupload"
				  :before-upload="beforeupload"
				  :headers="headers">
				  <el-button size="small" type="primary">上传</el-button>
				</el-upload>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qrcodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="唯一编码值" align="center" prop="code" />
      <el-table-column label="高速公路" align="center" prop="expressWayName" />
      <el-table-column label="桩号千米" align="center" prop="expressWayKil" />
      <el-table-column label="桩号米数" align="center" prop="expressWayMeter" />
      <el-table-column label="经度" align="center" prop="lon" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <!--<el-table-column label="二维码图片地址" align="center" prop="imageUrl" />-->
      <el-table-column label="二维码图片地址" align="center" prop="imageUrl">
				<template slot-scope="scope">
					<el-image 
				    style="width: 80px; height: 80px"
				    :src="scope.row.imageUrl" 
				    :preview-src-list="[scope.row.imageUrl]">
				  </el-image>
				</template>
			</el-table-column>
      <el-table-column label="方向类型" align="center" prop="direction" :formatter="directionFormat" />
      <el-table-column label="提醒内容" align="center" prop="remindContent" />
      <el-table-column label="二维码状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:qrcode:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:qrcode:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改扫码报警对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--<el-form-item label="唯一编码值" prop="code">
          <el-input v-model="form.code" placeholder="请输入唯一编码值" />
        </el-form-item>-->
        <el-form-item label="高速公路" prop="expressWayName">
          <el-input v-model="form.expressWayName" placeholder="请输入高速公路" />
        </el-form-item>
        <el-form-item label="桩号千米" prop="expressWayKil">
          <el-input type="number" v-model="form.expressWayKil" placeholder="请输入桩号千米" />
        </el-form-item>
        <el-form-item label="桩号米数" prop="expressWayMeter">
          <el-input type="number" v-model="form.expressWayMeter" placeholder="请输入桩号米数" />
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" oninput="value=value.replace(/[^\d^\.]/g,'')" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" oninput="value=value.replace(/[^\d^\.]/g,'')" placeholder="请输入纬度" />
        </el-form-item>
        <!--<el-form-item label="二维码图片地址" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入二维码图片地址" />
        </el-form-item>-->
        <el-form-item label="方向类型">
          <el-radio-group v-model="form.direction">
            <el-radio
              v-for="dict in directionOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提醒内容" prop="remindContent">
          <el-input v-model="form.remindContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="二维码状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--<el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="批量生成" top="40vh" :visible.sync="batch" width="500px" append-to-body style="margin-top: 200px;">
    	<el-form ref="form" label-width="80px">
        <el-form-item label="生成数">
          <el-input v-model="batchnum" type="number" placeholder="请输入生成数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchgeneration">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQrcode, getQrcode, delQrcode, addQrcode, updateQrcode, exportQrcode, qrCodeImage, createQrCodeImage } from "@/api/callPolice/qrcode";
import { getToken } from '@/utils/auth';
import { downLoadQrcodeZip } from "@/utils/zipdownload";
import url from '@/api/config.js';
export default {
  name: "Qrcode",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扫码报警表格数据
      qrcodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 方向类型 1:上行，2：下行字典
      directionOptions: [],
      // 二维码状态 1，启用，0，未启用，2，未下载制作，3已下载制作字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        expressWayName: null,
        expressWayKil: null,
        expressWayMeter: null,
        direction: null,
        remindContent: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      batch: false,
      batchnum: '',
      fileList: [],
      uploadUrl: process.env.VUE_APP_BASE_API + '/callPolice/manager/qrcode/analysisQrCodeZip',
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("qrcode_direction").then(response => {
      this.directionOptions = response.data;
    });
    this.getDicts("qrcode_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询扫码报警列表 */
    getList() {
      this.loading = true;
      listQrcode(this.queryParams).then(response => {
      	for(var i=0;i<response.rows.length;i++){
					response.rows[i].imageUrl = process.env.VUE_APP_BASE_QRCODE + response.rows[i].imageUrl
				}
        this.qrcodeList = response.rows;
        console.log(this.qrcodeList)
        this.total = response.total;
        this.loading = false;
      });
    },
    successupload(response, file, fileList){
    	console.log(response, file, fileList);
    	this.loading = false;
    	this.getList();
    	this.msgSuccess("上传结果： 成功" + response.data.successCount + '个，失败'+ response.data.failCount + '个');
    },
    beforeupload(file){
    	console.log(file);
    	this.loading = true;
    },
    // 方向类型 1:上行，2：下行字典翻译
    directionFormat(row, column) {
      return this.selectDictLabel(this.directionOptions, row.direction);
    },
    // 二维码状态 1，启用，0，未启用，2，未下载制作，3已下载制作字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    close(){
    	this.batch = false;
      this.batchnum = '';
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        expressWayName: null,
        expressWayKil: null,
        expressWayMeter: null,
        lon: null,
        lat: null,
        imageUrl: null,
        direction: 0,
        remindContent: null,
        status: 0,
        createTime: null,
        updateTime: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扫码报警";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQrcode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扫码报警";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQrcode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQrcode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除扫码报警编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQrcode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有扫码报警数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportQrcode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    
    /** 下载 */
    handledownload(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认下载所选数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          downLoadQrcodeZip('/callPolice/manager/qrcode/qrCodeImage/' + ids)	        
        })
    },
    /** 批量生成 */
    batchgeneration() {
      createQrCodeImage(this.batchnum).then(response => {
      	this.getList();
      	this.batch = false;
        this.msgSuccess("批量生成完成");
      });;
    }
  }
};
</script>
